package com.guojian.http.handle.service;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.eclipse.jetty.server.Request;

import com.guojian.common.http.HttpUtils;
import com.guojian.http.handle.AbstractJettyProxyHandler;
import com.guojian.http.handle.HttpJettyRequestBuilder;
import com.guojian.http.handle.HttpJettyResponseBuilder;
import com.guojian.http.handle.HttpJettyServletResponseWrapper;

public class HttpJettyHttpclientServiceHandle extends AbstractJettyProxyHandler<HttpUriRequest, CloseableHttpResponse> {

	//private HttpJettyAfterHandle afterHandle = new HttpJettyAfterHandle();

	private String targetUrl;

	/*  
	 * 将handleJettyRequestResponse进行了再次抽象.
	 * handleJettyRequestResponse可以将Jetty的请求进行任意构造,不局限于构造代理,
	 * 所以AbstractJettyProxyHandler成为了AbstractJettyHandler的子类
	 * AbstractJettyProxyHandler中的handleJettyRequestResponse,默认构造了代理流程的模板,后续只需要继承AbstractJettyProxyHandler
	 * 实际请求转换的方法,即可实现代理请求.此次用httpClient实现了代理请求,同样也可以使用okHttp,jdkConnection
	 * 
	 */

	/*@Override
	public void handleJettyRequestResponse(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		HttpUriRequest httpUriRequest = jettyRequestToHttpClientRequestConvert(baseRequest);
		CloseableHttpResponse closeableHttpResponse = doHttpClientProcess(httpUriRequest);
		CloseableHttpResponseToJettyResponseConvert(response, closeableHttpResponse);
	}
	
	public HttpUriRequest jettyRequestToHttpClientRequestConvert(Request baseRequest) {
		HttpUriRequest httpUriRequest = HttpJettyRequestBuilder.builder().httpJettyRequest(baseRequest).targetUrl(getTargetUrl()).build().buildRequest();
		return httpUriRequest;
	}
	
	public CloseableHttpResponse doHttpClientProcess(HttpUriRequest httpUriRequest) {
		CloseableHttpResponse closeableHttpResponse = HttpUtils.doHttpClientProcess(httpUriRequest);
		return closeableHttpResponse;
	}
	
	public void CloseableHttpResponseToJettyResponseConvert(HttpServletResponse response, CloseableHttpResponse closeableHttpResponse) throws IOException {
		HttpJettyServletResponseWrapper httpJettyServletResponseWrapper = HttpJettyResponseBuilder.builder().closeableHttpResponse(closeableHttpResponse).httpServletResponse(response).build().buildHttpServletResponse();
		httpJettyServletResponseWrapper.writeResponse();
	}*/

	@Override
	protected HttpUriRequest jettyRequestToTargetRequestConvert(Request baseRequest) {
		HttpUriRequest httpUriRequest = HttpJettyRequestBuilder.builder().httpJettyRequest(baseRequest).targetUrl(getTargetUrl()).build().buildRequest();
		return httpUriRequest;
	}

	@Override
	protected CloseableHttpResponse doHttpClientProcess(HttpUriRequest httpUriRequest) {
		CloseableHttpResponse closeableHttpResponse = HttpUtils.doHttpClientProcess(httpUriRequest);
		return closeableHttpResponse;
	}

	@Override
	protected void targetHttpResponseToJettyResponseConvert(HttpServletResponse response, CloseableHttpResponse targetHttpResponse) throws IOException {
		HttpJettyServletResponseWrapper httpJettyServletResponseWrapper = HttpJettyResponseBuilder.builder().closeableHttpResponse(targetHttpResponse).httpServletResponse(response).build().buildHttpServletResponse();
		httpJettyServletResponseWrapper.writeResponse();
	}

	public String getTargetUrl() {
		return targetUrl;
	}

	public HttpJettyHttpclientServiceHandle setTargetUrl(String targetUrl) {
		this.targetUrl = targetUrl;
		return this;
	}

}
